﻿//********************************************************************************//
//创建日期：郭登杰 2016-1-12
//功能说明：退款确认
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Access;
using BLL;
using Web.Class;
using Entity;
using DevExpress.Web;

namespace Web.Pages.QuShui._6.ZhangWuGL
{
    public partial class TuiKuanQR : BasePage
    {
        #region 属性

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据库操作对象
        /// </summary>
        SpHelper _sphelper = new SpHelper();

        /// <summary>
        /// 来函信息数据处理
        /// </summary>
        YS_LH_LAIHANXXProcess _YS_LH_LAIHANXXBLL = new YS_LH_LAIHANXXProcess();

        #endregion

        #region 事件

        /// <summary>
        /// 加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                LoadData(true);
            }
        }
     
        /// <summary>
        /// 回调响应
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            //判断是否有勾选
            List<QS_LH_TUIKUANXX> list = this.SelectedTuiKuanXXList();
            if (list.Count == 0)
            {
                e.Result = e.Parameter + "|-1|" + "未选择要操作的记录。";
                return;
            }
            if (e.Parameter == "quxiao")
            {
                string rsno = "-1";
                string rsmsg = "";
                for (int i = 0; i < list.Count; i++)
                {
                    this._sphelper.P_QSZW_TUIKUANQX(list[i].S_LAIHANBH,LoginInfo.CurrentUser.UserID.ToString(),out rsno,out rsmsg);
                    if(rsno!="1")
                    {
                        e.Result = e.Parameter + "|-1|" + "异常,操作失败。";
                        return;
                    }
                }
                e.Result = e.Parameter + "|0|" + "操作成功。";
            }
            if (e.Parameter == "queren")
            {
                string rsno="-1";
                string rsmsg="";
                int successCount = 0;
                int errorCount =0;
                int ignoreCount=0;
                for (int i = 0; i < list.Count; i++)
                {
                    this._sphelper.P_QSZW_TUIKUANQR(list[i].S_LAIHANBH,LoginInfo.CurrentUser.UserID.ToString(),out rsno,out rsmsg);
                    if (rsno == "1")
                    {
                        successCount++;
                    }
                    else if (rsno == "-1")
                    {
                        errorCount++;
                    }
                    else if (rsno == "0")
                    {
                        ignoreCount++;
                    }                   
                }
                string msg = e.Parameter + "|0|";
                if(successCount>0)
                {
                    msg = msg + " " + successCount + "笔退款确认成功";
                }
                if (ignoreCount > 0)
                {
                    msg = msg + " " + ignoreCount + "笔退款已确认,无需重复操作";
                }
                if (errorCount > 0)
                {
                    msg = msg + " " + errorCount + "笔退款确认操作异常";
                }
                e.Result = msg;
            }
        }

        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerform_Click(object sender, EventArgs e)
        {
            LoadData(true);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            //加载退款类型
            WordsManager manger = new WordsManager();
            PageTools.QSLoadCombox(this.cmbTuiKuanLX,PageTools.eLoadType.退款类型QS,null,"");
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="reget">是否重新获取</param>
        private void LoadData(bool reget)
        {
            if (reget)
            {
                this.dtSource = this._sphelper.P_QSZW_TUIKUANQRLIST(this.txtYongHuH.Text.Trim() == "" ? null : this.txtYongHuH.Text.Trim(),
                                                                    this.txtYongHuM.Text.Trim() == "" ? null : this.txtYongHuM.Text.Trim(),
                                                                    this.txtYM.Text.Trim() == "" ? null : (int?)int.Parse(this.txtYM.Text.Trim()),
                                                                    this.cmbTuiKuanLX.Text == "" ? null : (int?)Convert.ToInt32(this.cmbTuiKuanLX.Value),
                                                                    this.txtLaiHanBH.Text.Trim() == "" ? null : this.txtLaiHanBH.Text.Trim()).Tables[0];
            }
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            this.Binding();
        }

        /// <summary>
        /// 获取选中退款信息列表
        /// </summary>
        private List<QS_LH_TUIKUANXX> SelectedTuiKuanXXList()
        {
            List<QS_LH_TUIKUANXX> list = new List<QS_LH_TUIKUANXX>();
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox ck = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cid");
                if (ck.Checked)
                {
                    QS_LH_TUIKUANXX obj = new QS_LH_TUIKUANXX();
                    object[] vals = (object[])this.gvList.GetRowValues(i, "S_LAIHANBH", "I_TUIKUANZT");
                    obj.S_LAIHANBH = vals[0].ToString();
                    obj.I_TUIKUANZT = Convert.ToInt32(vals[1]);
                    list.Add(obj);
                }
            }
            return list;
        }

        /// <summary>
        /// 绑定设置
        /// </summary>
        private void Binding()
        {
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox ck = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cid");
                //HiddenField hfTuiKuanZT = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfTuiKuanZT");
                //ck.Visible = hfTuiKuanZT.Value == "0";
            }
        }

        #endregion
    }
}